<?php
class MStaff extends CI_Model{

	public function __construct()
    {
        parent::__construct();
    }
	function getUser($id) {
		$data = array();
	    $this->db->where('UserId',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('user');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;      
	}
    public function getLastSID($start, $end)
    {
        $query = "SELECT SID from `staff` ORDER BY `StaffID` DESC LIMIT ".$start.",".$end;
        $result =  $this->db->query($query);
        $row = $result->row();
        return $row->SID;
    }
    
	function getAllStaffActive()
    {
        $data = array();
        $this->db->where('SActive',1);
	    $Q = $this->db->get('staff');
        if ($Q->num_rows() > 0){
    	   foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
        }
        $Q->free_result();    
        return $data;    
    }
    
	function getDocumentTypeDropDown()
	{
		 $data = array();
		 $this->db->where('DTActive',1);
		 $this->db->order_by("DocumentType", "ASC");
	     $Q = $this->db->get('documenttype');
	     if ($Q->num_rows() > 0){
	       $data[0] = '...select type...';
	       foreach ($Q->result_array() as $row){
	         $data[$row['DocumentTypeID']] = $row['DocumentType'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
	}
    
    function getLocationDropDown()
	{
		 $data = array();
		 $this->db->where('LActive',1);
		 $this->db->order_by("Location", "ASC");
	     $Q = $this->db->get('location');
	     if ($Q->num_rows() > 0){
	       $data[0] = '...select type...';
	       foreach ($Q->result_array() as $row){
	         $data[$row['LocationID']] = $row['Location'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
	}
    
    function getContractTypeDropDown()
	{
		 $data = array();
		 $this->db->where('CTActive',1);
		 $this->db->order_by("ContractType", "ASC");
	     $Q = $this->db->get('contracttype');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['CTID']] = $row['ContractType'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
	}
    
    function getWorkTypeDropDown()
	{
		 $data = array();
		 $this->db->where('WTActive',1);
		 $this->db->order_by("WorkType", "ASC");
	     $Q = $this->db->get('worktype');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['WorkTypeID']] = $row['WorkType'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
	}
	
	function getStaffDropDown()
	{
		 $data = array();
		 $this->db->where('SActive',1);
		 $this->db->order_by("FirstName", "ASC");
	     $Q = $this->db->get('staff');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['StaffID']] = $row['FirstName'] . ' ' . $row['LastName'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
	}
	function getStatusDropdown()
	{
		 $data = array();
	  	 $this->db->order_by("MarriedStatus", "ASC");
	     $Q = $this->db->get('marriedstatus');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['MSID']] = $row['MarriedStatus'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
	}    
	
	function getAllProfiles($strSearch, $team = 0){
	     $data = array();
	     $this->db->select('staff.StaffID, staff.FirstName, staff.MiddleName, staff.LastName, staff.NickName, staff.StaffType, position.Title, user.UserId,
				team.TeamName, staff.CurrentAddress,staff.PhoneNumber,staff.SkypeID,staff.Email, staff.Comment, staff.Photo, location.Location');
	     $this->db->from('staff');
	     $this->db->join('position','position.PositionID = staff.PositionID','left');
	     $this->db->join('team', 'staff.TeamID = team.TeamID','left');
         $this->db->join('user', 'user.StaffId = staff.StaffID', 'left');
         $this->db->join('location','staff.LocationId = location.LocationID','left');
         if($team > 0)
         {
            $this->db->where('team.TeamID', db_clean($team));
         }
         if($strSearch != null && $strSearch != '') 
         {
            $this->db->where("(staff.FirstName LIKE '%".$strSearch."%' OR staff.LastName LIKE '%".$strSearch."%' OR staff.Email LIKE '%".$strSearch."%')");
             //$this->db->like('staff.FirstName', $strSearch);
             //$this->db->or_like('staff.LastName', $strSearch);
             //$this->db->or_like('staff.Email', $strSearch);
         } 
	     $this->db->order_by('staff.CreatedDate', 'DESC');
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
	}
    
	   function getAllEndprobationStaffs($team = 0){
	     $data = array();
	     $this->db->select('staff.StaffID, staff.FirstName, staff.MiddleName, staff.LastName, staff.NickName, staff.StaffType, position.Title, user.UserId,
				team.TeamName, staff.CurrentAddress,staff.PhoneNumber,staff.SkypeID,staff.Email, staff.Comment, staff.Photo, location.Location, staff.EndProbation');
	     $this->db->from('staff');
	     $this->db->join('position','position.PositionID = staff.PositionID','left');
	     $this->db->join('team', 'staff.TeamID = team.TeamID','left');
         $this->db->join('user', 'user.StaffId = staff.StaffID', 'left');
         $this->db->join('location','staff.LocationId = location.LocationID','left');
         $this->db->where('staff.EndProbation < CURDATE()');
         if($team > 0)
         {
            $this->db->where('team.TeamID', db_clean($team));
         }
	     $this->db->order_by('staff.CreatedDate', 'DESC');
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
	}
	
	function getStaffDetailById($id){
	     $data = array();
	     $this->db->select('user.Username, user.Phone, user.Password, user.UserTypeID,staff.*, usertype.UserType, marriedstatus.MarriedStatus,position.Title, team.TeamName, location.Location');
	     $this->db->from('user');
	     $this->db->join('usertype', 'user.UserTypeID = usertype.UserTypeID');
	     $this->db->join('staff','user.StaffId = staff.StaffID','right');
	     $this->db->join('marriedstatus','staff.MaritalStatus = marriedstatus.MSID');
         $this->db->join('position','position.PositionID = staff.PositionID','left');
	     $this->db->join('team', 'staff.TeamID = team.TeamID','left');
         $this->db->join('location','staff.LocationId = location.LocationID','left');
	     $this->db->where('staff.StaffID',id_clean($id));
	     $this->db->order_by('usertype.UserType', 'ASC');
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
		    $data = $Q->row_array();
	     }
	     $Q->free_result();    
	     return $data;    
	}
    
    function getAccountDetailByUId($id){
	     $data = array();
	     $this->db->select('user.Username, user.Phone, user.Password, user.UserTypeID,staff.*, usertype.UserType, marriedstatus.MarriedStatus,position.Title, team.TeamName, location.Location');
	     $this->db->from('user');
	     $this->db->join('usertype', 'user.UserTypeID = usertype.UserTypeID');
	     $this->db->join('staff','user.StaffId = staff.StaffID','right');
	     $this->db->join('marriedstatus','staff.MaritalStatus = marriedstatus.MSID');
         $this->db->join('position','position.PositionID = staff.PositionID','left');
	     $this->db->join('team', 'staff.TeamID = team.TeamID','left');
         $this->db->join('location','staff.LocationId = location.LocationID','left');
	     $this->db->where('user.UserId',id_clean($id));
	     $this->db->order_by('usertype.UserType', 'ASC');
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
		    $data = $Q->row_array();
	     }
	     $Q->free_result();    
	     return $data;    
	}

    function addStaff()
    {
        $data = array( 
			'SID' => db_clean($_POST['SID']),
			'FirstName' => db_clean($_POST['FirstName']),
			'LastName' => db_clean($_POST['LastName']),
			'MiddleName' => db_clean($_POST['MiddleName']),
            'NickName' => db_clean($_POST['NickName']),
            'Gender' => db_clean($_POST['Gender']),
            'DoB' => db_clean($_POST['DoB']),
            'PoB' => db_clean($_POST['PoB']),
            'MaritalStatus' => db_clean($_POST['MSID']),
            'NoChildren' => db_clean($_POST['NoChildren']),
            'CurrentAddress' => db_clean($_POST['CurrentAddress']),
            'PhoneNumber' => db_clean($_POST['Phone']),
            'Email' => db_clean($_POST['Email']),
            'SkypeID' => db_clean($_POST['SkypeID']),
            'FriendEmail' => db_clean($_POST['FriendEmail']),
            'StartDate' => db_clean($_POST['StartDate']),
            'EndProbation' => db_clean($_POST['EndProbation']),
            'Comment' => db_clean($_POST['Comment']),
            'PositionID' => id_clean($_POST['PositionID']),
            'TeamID' => db_clean($_POST['TeamID']),
            'SActive' => db_clean(isset($_POST['activate']) ? 1 : 0),
            'LocationId' => db_clean($_POST['LocationId'])
		);
		if ($_FILES){
		 	$path_photo = 'images/photo';
            $path_idcopy = 'images/idcopy';
            $path_passportcopy = 'images/passportcopy';
		 	if(!is_dir($path_photo))
		    {
		      mkdir($path_photo,0755,TRUE);
		    } 
            if(!is_dir($path_idcopy))
		    {
		      mkdir($path_idcopy,0755,TRUE);
		    } 
            if(!is_dir($path_passportcopy))
		    {
		      mkdir($path_passportcopy,0755,TRUE);
		    } 
		    
			$config['upload_path'] = './'.$path_photo;
			$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|ppt';// 'gif|jpg|png|flv|mp4|pdf|ppt';
			$config['max_size'] = '1024';//512mb = 524288
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
			if (strlen($_FILES['Photo']['name'])){
				if(!$this->upload->do_upload('Photo')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['Photo'] = $image['file_name'];
				}
			}
            
           	$config['upload_path'] = './'.$path_idcopy;
			$this->upload->initialize($config); 
			if (strlen($_FILES['IDCopy']['name'])){
				if(!$this->upload->do_upload('IDCopy')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['IDCopy'] = $image['file_name'];
				}
			}
            
            $config['upload_path'] = './'.$path_passportcopy;
			$this->upload->initialize($config); 
			if (strlen($_FILES['PassportCopy']['name'])){
				if(!$this->upload->do_upload('PassportCopy')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['PassportCopy'] = $image['file_name'];
				}
			}
		}
        $this->db->set('CreatedDate', 'NOW()', FALSE);
		$this->db->insert('staff', $data);
        $staffId = $this->db->insert_id();
        //$this->load->library('usermodel',NULL,'user');
        $this->load->library('usermodel');
        $this->usermodel->setUserName($_POST['Username']);
        $this->usermodel->setPassword($_POST['Password']);
        $this->usermodel->setEmail($_POST['Email']);
        $this->usermodel->setPhone($_POST['Phone']);
        $this->usermodel->setUserTypeID($_POST['UserTypeID']);
        $this->usermodel->setActivate(isset($_POST['activate']) ? 1 : 0);
        $this->usermodel->setStaffID($staffId);
        $this->MUser->addUser($this->usermodel);
    }

	function updateStaff()
    {
        $data = array( 
			'SID' => db_clean($_POST['SID']),
			'FirstName' => db_clean($_POST['FirstName']),
			'LastName' => db_clean($_POST['LastName']),
			'MiddleName' => db_clean($_POST['MiddleName']),
            'NickName' => db_clean($_POST['NickName']),
            'Gender' => db_clean($_POST['Gender']),
            'DoB' => db_clean($_POST['DoB']),
            'PoB' => db_clean($_POST['PoB']),
            'MaritalStatus' => db_clean($_POST['MSID']),
            'NoChildren' => db_clean($_POST['NoChildren']),
            'CurrentAddress' => db_clean($_POST['CurrentAddress']),
            'PhoneNumber' => db_clean($_POST['Phone']),
            'Email' => db_clean($_POST['Email']),
            'SkypeID' => db_clean($_POST['SkypeID']),
            'FriendEmail' => db_clean($_POST['FriendEmail']),
            'StartDate' => db_clean($_POST['StartDate']),
            'EndProbation' => db_clean($_POST['EndProbation']),
            'Comment' => db_clean($_POST['Comment']),
            'PositionID' => id_clean($_POST['PositionID']),
            'TeamID' => db_clean($_POST['TeamID']),
            'SActive' => db_clean(isset($_POST['activate']) ? 1 : 0),
            'LocationId' => db_clean($_POST['LocationId'])
		);
		if ($_FILES){
		 	$path_photo = 'images/photo';
            $path_idcopy = 'images/idcopy';
            $path_passportcopy = 'images/passportcopy';
		 	if(!is_dir($path_photo))
		    {
		      mkdir($path_photo,0755,TRUE);
		    } 
            if(!is_dir($path_idcopy))
		    {
		      mkdir($path_idcopy,0755,TRUE);
		    } 
            if(!is_dir($path_passportcopy))
		    {
		      mkdir($path_passportcopy,0755,TRUE);
		    } 
		    
			$config['upload_path'] = './'.$path_photo;
			$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|ppt';// 'gif|jpg|png|flv|mp4|pdf|ppt';
			$config['max_size'] = '1024';//512mb = 524288
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
			if (strlen($_FILES['Photo']['name'])){
				if(!$this->upload->do_upload('Photo')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['Photo'] = $image['file_name'];
				}
			}
            
           	$config['upload_path'] = './'.$path_idcopy;
			$this->upload->initialize($config); 
			if (strlen($_FILES['IDCopy']['name'])){
				if(!$this->upload->do_upload('IDCopy')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['IDCopy'] = $image['file_name'];
				}
			}
            
            $config['upload_path'] = './'.$path_passportcopy;
			$this->upload->initialize($config); 
			if (strlen($_FILES['PassportCopy']['name'])){
				if(!$this->upload->do_upload('PassportCopy')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['PassportCopy'] = $image['file_name'];
				}
			}
		}
        $this->db->set('ModifiedDate', 'NOW()', FALSE);
        $this->db->where('StaffID',id_clean($_POST['StaffID']));
		$this->db->update('staff', $data);

		$staffId = $_POST['StaffID'];
        $this->load->library('usermodel');
        $this->usermodel->setUserName($_POST['Username']);
        $this->usermodel->setPassword(strlen($_POST['Password']) ? $_POST['Password'] : null);
        $this->usermodel->setEmail($_POST['Email']);
        $this->usermodel->setPhone($_POST['Phone']);
        $this->usermodel->setUserTypeID($_POST['UserTypeID']);
        $this->usermodel->setActivate(isset($_POST['activate']) ? 1 : 0);
        $this->usermodel->setStaffID($staffId);
        $this->MUser->updateUser($this->usermodel);
    }
    
    function updateAccountSetting()
    {
        $data = array( 
			'FirstName' => db_clean($_POST['FirstName']),
			'LastName' => db_clean($_POST['LastName']),
			'MiddleName' => db_clean($_POST['MiddleName']),
            'NickName' => db_clean($_POST['NickName']),
            'Gender' => db_clean($_POST['Gender']),
            'DoB' => db_clean($_POST['DoB']),
            'PoB' => db_clean($_POST['PoB']),
            'MaritalStatus' => db_clean($_POST['MSID']),
            'NoChildren' => db_clean($_POST['NoChildren']),
            'CurrentAddress' => db_clean($_POST['CurrentAddress']),
            'PhoneNumber' => db_clean($_POST['Phone']),
            'Email' => db_clean($_POST['Email']),
            'SkypeID' => db_clean($_POST['SkypeID']),
            'FriendEmail' => db_clean($_POST['FriendEmail']),
            'Comment' => db_clean($_POST['Comment'])
		);
		if ($_FILES){
		 	$path_photo = 'images/photo';
            $path_idcopy = 'images/idcopy';
            $path_passportcopy = 'images/passportcopy';
		 	if(!is_dir($path_photo))
		    {
		      mkdir($path_photo,0755,TRUE);
		    } 
            if(!is_dir($path_idcopy))
		    {
		      mkdir($path_idcopy,0755,TRUE);
		    } 
            if(!is_dir($path_passportcopy))
		    {
		      mkdir($path_passportcopy,0755,TRUE);
		    } 
		    
			$config['upload_path'] = './'.$path_photo;
			$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|ppt';// 'gif|jpg|png|flv|mp4|pdf|ppt';
			$config['max_size'] = '1024';//512mb = 524288
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
			if (strlen($_FILES['Photo']['name'])){
				if(!$this->upload->do_upload('Photo')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['Photo'] = $image['file_name'];
				}
			}
            
           	$config['upload_path'] = './'.$path_idcopy;
			$this->upload->initialize($config); 
			if (strlen($_FILES['IDCopy']['name'])){
				if(!$this->upload->do_upload('IDCopy')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['IDCopy'] = $image['file_name'];
				}
			}
            
            $config['upload_path'] = './'.$path_passportcopy;
			$this->upload->initialize($config); 
			if (strlen($_FILES['PassportCopy']['name'])){
				if(!$this->upload->do_upload('PassportCopy')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['PassportCopy'] = $image['file_name'];
				}
			}
		}
        $this->db->set('ModifiedDate', 'NOW()', FALSE);
        $this->db->where('StaffID',id_clean($_POST['StaffID']));
		$this->db->update('staff', $data);

		$staffId = $_POST['StaffID'];
        $this->load->library('usermodel');
        $this->usermodel->setUserName($_POST['Username']);
        $this->usermodel->setPassword(strlen($_POST['Password']) ? $_POST['Password'] : null);
        $this->usermodel->setEmail($_POST['Email']);
        $this->usermodel->setPhone($_POST['Phone']);
        $this->usermodel->setUserTypeID($_SESSION['role_id']);
        $this->usermodel->setActivate(1);
        $this->usermodel->setStaffID($staffId);
        $this->MUser->updateUser($this->usermodel);
    }
    
    function getAllStaffDocument(){
         $data = array();
	     $this->db->select('staffdocument.*, staff.FirstName, staff.LastName, documenttype.DocumentType as DocumentTitle');
	     $this->db->from('staffdocument');
	     $this->db->join('staff','staffdocument.StaffID = staff.StaffID');
	     $this->db->join('documenttype', 'documenttype.DocumentTypeID = staffdocument.DocumentType');
	     $this->db->order_by('documenttype.DocumentType');
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
    }
    
    function getStaffDocumentByStaffId($id){
         $data = array();
	     $this->db->select('staffdocument.*, documenttype.DocumentType as DocumentTitle');
	     $this->db->from('staffdocument');
	     $this->db->join('documenttype', 'documenttype.DocumentTypeID = staffdocument.DocumentType');
         $this->db->where('staffdocument.StaffID',id_clean($id));
	     $this->db->order_by('staffdocument.CreateDate','DESC');
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
    }
    
    function getStaffDocumentByStaffIdDocId($staffId, $staffDocId){
         $data = array();
	     $this->db->select('staffdocument.*, documenttype.DocumentType as DocumentTitle');
	     $this->db->from('staffdocument');
	     $this->db->join('documenttype', 'documenttype.DocumentTypeID = staffdocument.DocumentType');
         $this->db->where('staffdocument.StaffID',id_clean($staffId));
         $this->db->where('staffdocument.StaffDocID',id_clean($staffDocId));
	     $this->db->order_by('documenttype.DocumentType');
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       $data = $Q->row_array();
	     }
	     $Q->free_result();    
	     return $data; 	
    }
    
    function createStaffDocument($id) {
        $data = array( 
            'StaffID' => id_clean($id),
            'DocumentType' => id_clean($_POST['DocumentTypeID']),
            'Remark' => db_clean($_POST['Remark']),
            'DateReceived' => db_clean($_POST['DateReceived'])
		);
		if ($_FILES){
		 	$path_document = 'images/staffdocument';
		 	if(!is_dir($path_document))
		    {
		      mkdir($path_document,0755,TRUE);
		    } 	    
			$config['upload_path'] = './'.$path_document;
			$config['allowed_types'] = '*';// 'gif|jpg|png|flv|mp4|pdf|ppt';
			$config['max_size'] = '1024';//512mb = 524288
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
			if (strlen($_FILES['DocumentPath']['name'])){
				if(!$this->upload->do_upload('DocumentPath')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['DocumentPath'] = $image['file_name'];
				}
			}
		}
        $this->db->set('CreateDate', 'NOW()', FALSE);
		$this->db->insert('staffdocument', $data);
    }
    
    function updateStaffDocument($staffId, $staffDocId) {
        $data = array( 
            'DocumentType' => id_clean($_POST['DocumentTypeID']),
            'Remark' => db_clean($_POST['Remark']),
            'DateReceived' => db_clean($_POST['DateReceived'])
		);
		if ($_FILES){
		 	$path_document = 'images/staffdocument';
		 	if(!is_dir($path_document))
		    {
		      mkdir($path_document,0755,TRUE);
		    } 	    
			$config['upload_path'] = './'.$path_document;
			$config['allowed_types'] = '*';// 'gif|jpg|png|flv|mp4|pdf|ppt';
			$config['max_size'] = '1024';//512mb = 524288
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
			if (strlen($_FILES['DocumentPath']['name'])){
				if(!$this->upload->do_upload('DocumentPath')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
				if ($image['file_name']){
					$data['DocumentPath'] = $image['file_name'];
				}
			}
		}
        $this->db->set('ModifiedDate', 'NOW()', FALSE);
        $this->db->where('StaffDocID',id_clean($staffDocId));
        $this->db->where('StaffID',id_clean($staffId));
		$this->db->update('staffdocument', $data);
    }
    
    function getStaffContractByStaffId($id){
         $data = array();
	     $this->db->select('staffcontract.*, contracttype.ContractType, position.Title, team.TeamName, worktype.WorkType');
	     $this->db->from('staffcontract');
	     $this->db->join('contracttype', 'contracttype.CTID = staffcontract.ContractTypeID');
         $this->db->join('worktype', 'worktype.WorkTypeID = staffcontract.WorkTypeID');
         $this->db->join('position', 'position.PositionID = staffcontract.PositionID','left');
         $this->db->join('team', 'team.TeamID = staffcontract.Team','left');
         $this->db->where('staffcontract.StaffID',id_clean($id));
	     $this->db->order_by('staffcontract.CreatedDate','DESC');
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
    }
    
    function getStaffContractByStaffIdContractId($staffId, $staffContractId){
         $data = array();
	     $this->db->select('staffcontract.*, contracttype.ContractType, position.Title, team.TeamName, worktype.WorkType');
	     $this->db->from('staffcontract');
	     $this->db->join('contracttype', 'contracttype.CTID = staffcontract.ContractTypeID');
         $this->db->join('worktype', 'worktype.WorkTypeID = staffcontract.WorkTypeID');
         $this->db->join('position', 'position.PositionID = staffcontract.PositionID','left');
         $this->db->join('team', 'team.TeamID = staffcontract.Team','left');
         $this->db->where('staffcontract.StaffID',id_clean($staffId));
         $this->db->where('staffcontract.StaffContractID',id_clean($staffContractId));
	     $this->db->order_by('staffcontract.CreatedDate','DESC');
         $this->db->limit(1);
	     $Q = $this->db->get();
         if ($Q->num_rows() > 0){
	        $data = $Q->row_array();
	     }
	     $Q->free_result();    
	     return $data;
    }
    
    function createStaffContract($id) {
        $data = array( 
            'StaffID' => id_clean($id),
            'StartContract' => db_clean($_POST['StartContract']),
            'EndContract' => $_POST['ContractTypeID'] == 1 ? db_clean($_POST['EndContract']) : '1900-01-01',
            'ContractTypeID' => id_clean($_POST['ContractTypeID']),
            'WorkTypeID' => id_clean($_POST['WorkTypeID']),
            'PositionID' => id_clean($_POST['PositionID']),
            'Team' => id_clean($_POST['TeamID']),
            'Salary' => db_clean($_POST['Salary']),
            'PhoneAllowance' => db_clean($_POST['PhoneAllowance']),
            'FuelAllowance' => db_clean($_POST['FuelAllowance']),
            'InsuranceStart' => db_clean($_POST['InsuranceStart']),
            'Remark' => db_clean($_POST['Remark']),
            'SCActive' => db_clean(isset($_POST['SCActive']) ? 1 : 0)
		);
        $this->db->set('CreatedDate', 'NOW()', FALSE);
		$this->db->insert('staffcontract', $data);
        
        $contractId = $this->db->insert_id();
        $this->load->library('AppraisalModel');
        $this->load->library('AmendmentModel');
        $this->load->library('SalaryIncreaseModel');
        $this->load->helper('array');
        
        $salaryIncrease = $_POST['IncreasedDate'];
        $increasedComment = $_POST['SalaryIncreaseComment'];
        $salaryList = null;
        if(count($salaryIncrease) > 0) {
            $salaryList = array();
            for($i=0 ; $i<count($salaryIncrease); $i++){
                //$this->salaryincreasemodel->setIncreasedDate(db_clean($salaryIncrease[$i]));
                //$this->salaryincreasemodel->setComment(db_clean($increasedComment[$i]));
                //array_push($salaryList, $this->salaryincreasemodel);
                $salaryList[] = $salaryIncrease[$i];
                array_push($salaryList, $increasedComment[$i]);
            }
        }
        
        $appraisal = $_POST['AppraisalDate'];
        $appraisalComment = $_POST['AppraisalComment'];
        $appraisalList = null;
        if(count($appraisal) > 0){
            $appraisalList = array();
            for($i=0 ; $i<count($appraisal); $i++){
                $appraisalList[] = $appraisal[$i];
                array_push($appraisalList, $appraisalComment[$i]);
            }  
        }
        
        $amendment = $_POST['AmendmentDate'];
        $amendmentComment = $_POST['AmendmentComment'];
        $amendmentList = null;     
        if(count($amendment) > 0){
            $amendmentList = array();
            for($i=0 ; $i<count($amendment); $i++){
                $amendmentList[] = $amendment[$i];
                array_push($amendmentList, $amendmentComment[$i]);
            }  
        } 
        $this->MContract->createStaffContract($contractId, $salaryList, $appraisalList, $amendmentList);
    }
    
    function deleteStaffContract($staffId, $staffContractId){
	 	$this->db->where('StaffID', id_clean($staffId));
        $this->db->where('StaffContractID', id_clean($staffContractId));
		$this->db->delete('staffcontract');	
	 }
     
     function getStaffCheckListByStaffId($id)
     {
         $data = array();
         $this->db->where('staffchecklist.StaffID',id_clean($id));
	     $this->db->order_by('VisaDate','DESC');
	     $Q = $this->db->get('staffchecklist');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
     } 
     function getSingleStaffCheckListByStaffId($id)
     {
         $data = array();
         $this->db->where('staffchecklist.StaffID',id_clean($id));
	     $this->db->limit(1);
	     $Q = $this->db->get('staffchecklist');
	     if ($Q->num_rows() > 0){
	        $data = $Q->row_array();
	     }
	     $Q->free_result();    
	     return $data; 	
     } 
     function createStaffCheckList($id)
     {
         $data = array( 
            'StaffID' => id_clean($id),
            'Contract' => db_clean(isset($_POST['Contract']) ? 1:0),
            'CodeConduct' => db_clean(isset($_POST['CodeConduct']) ? 1:0),
            'ConflictInterest' => db_clean(isset($_POST['ConflictInterest']) ? 1:0),
            'IRMeeting' => db_clean(isset($_POST['IRMeeting']) ? 1:0),
            'SelfDecForm' => db_clean(isset($_POST['SelfDecForm']) ? 1:0),
            'PerInfoForm' => db_clean(isset($_POST['PerInfoForm']) ? 1:0),
            'Email' => db_clean(isset($_POST['Email']) ? 1:0),
            'BankAccount' => db_clean(isset($_POST['BankAccount']) ? 1:0),
            'StaffDisCard' => db_clean(isset($_POST['StaffDisCard']) ? 1:0),
            'SendBD' => db_clean(isset($_POST['SendBD']) ? 1:0),
            'StaffProfile' => db_clean(isset($_POST['StaffProfile']) ? 1:0),
            'InsuranceState' => db_clean(isset($_POST['InsuranceState']) ? 1:0),
            'Visa' => db_clean(isset($_POST['Visa']) ? 1:0),
            'VisaDate' => db_clean($_POST['VisaDate']),
            'Remark' => db_clean($_POST['Remark'])
		);
		$this->db->insert('staffchecklist', $data);
     }
     
     function updateStaffCheckListByStaffId($staffId)
     {
         $data = array( 
            'Contract' => db_clean(isset($_POST['Contract']) ? 1:0),
            'CodeConduct' => db_clean(isset($_POST['CodeConduct']) ? 1:0),
            'ConflictInterest' => db_clean(isset($_POST['ConflictInterest']) ? 1:0),
            'IRMeeting' => db_clean(isset($_POST['IRMeeting']) ? 1:0),
            'SelfDecForm' => db_clean(isset($_POST['SelfDecForm']) ? 1:0),
            'PerInfoForm' => db_clean(isset($_POST['PerInfoForm']) ? 1:0),
            'Email' => db_clean(isset($_POST['Email']) ? 1:0),
            'BankAccount' => db_clean(isset($_POST['BankAccount']) ? 1:0),
            'StaffDisCard' => db_clean(isset($_POST['StaffDisCard']) ? 1:0),
            'SendBD' => db_clean(isset($_POST['SendBD']) ? 1:0),
            'StaffProfile' => db_clean(isset($_POST['StaffProfile']) ? 1:0),
            'InsuranceState' => db_clean(isset($_POST['InsuranceState']) ? 1:0),
            'Visa' => db_clean(isset($_POST['Visa']) ? 1:0),
            'VisaDate' => db_clean($_POST['VisaDate']),
            'Remark' => db_clean($_POST['Remark'])
		);
        $this->db->where('StaffID',id_clean($staffId));
		$this->db->update('staffchecklist', $data);
     }
     
     function scanCheckListByStaffId($id)
     {
         $this->db->where("(StaffID=".$id." AND Contract=1 AND CodeConduct=1 AND ConflictInterest=1 AND IRMeeting=1 AND SelfDecForm=1 AND PerInfoForm=1 AND Email=1 AND BankAccount=1 AND StaffDisCard=1 AND SendBD=1 AND StaffProfile=1 AND InsuranceState=1 AND Visa=1)");
	     $this->db->limit(1);
	     $Q = $this->db->get('staffchecklist');    
	     return $Q->num_rows(); 	
     }
}
?>